//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using Style = CommunityServer.Controls.Style;

namespace CommunityServer.ControlPanel.Files
{
	/// <summary>
	/// Summary description for CategoryForm.
	/// </summary>
	public class CategoryForm : BaseFilesPage
	{
		protected Style ControlPanelStyle;
		protected FormLabel tt;
		protected TextBox CategoryName;
		protected FormLabel Formlabel1;
		protected TextBox CategoryDesc;
		protected FormLabel Formlabel2;
		protected CheckBox CategoryEnabled;
		protected RequiredFieldValidator RequiredFieldValidator1;
		protected MPContent bcr;
		protected MPContainer MPContainer;
		protected IButton Button1;
	
		private void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				PostCategory pc = GetCategory();	        
				CategoryName.Text = pc.Name;
				CategoryDesc.Text = pc.Description;
				CategoryEnabled.Checked = pc.IsEnabled;
			}

			Head.AddTitle("Tag Editor", this.Context);
		}

		protected PostCategory GetCategory()
		{
			PostCategory pc = null;

			if(Request.QueryString["CategoryID"] != null && Request.QueryString["CategoryID"] != "-1")
			{
                
				pc = PostCategories.GetCategory(Int32.Parse(Request.QueryString["CategoryID"]),CurrentFolder.SectionID,true);
				if(pc == null)
					throw new Exception("Sub-Folder could not be found");
			}
			else
			{
				pc = new PostCategory();
				pc.SectionID = CurrentFolder.SectionID;
            
			}

			return pc;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new EventHandler(this.Button1_Click);
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{
				PostCategory pc = GetCategory();
				pc.Name = Server.HtmlDecode(CategoryName.Text) ;
				pc.Description = CategoryDesc.Text;
				pc.IsEnabled = CategoryEnabled.Checked;

				if(pc.CategoryID > 0)
					PostCategories.UpdateCategory(pc);
				else
					PostCategories.CreateCategory(pc);

				Modal.ClosePage(Page) ;

			}
			
		}
	}
}

